<?php if(navigation::getAdminAction() === 'create' || navigation::getAdminAction() === 'update' ): ?>
<?php
if(navigation::getAdminModeleId()>0){
    $page = new modele_page(navigation::getAdminModeleId());
    $data = $page->getDatas();
    $data['permalien'] = $page->getPermalien()->getCleanedLien(false);
    if($data['summary']===$page->generateSummary()) $data['summary'] = '';
    $action = 'update';
}else{
    $action = 'create';
    $data = array();
}
?>
<div class="columns thirteen">
    <h1><a class="pwe-ui-btn" href="?template=page"><span class="ui-icon ui-icon ui-icon-arrowthick-1-w"></span></a> Modifier/créer une page statique</h1>
    <hr style="margin: 5px 0 30px 0;">
</div>
<form method="post" enctype="multipart/form-data" action="?template=page&action=<?php echo $action; ?>&modele=page&do=1&modele_id=<?php echo (isset($data['id']))?$data['id']:''; ?>">
    <div class="columns nine">
        <div align="right">
            <input type="submit" value="Enregistrer" class="pwe-ui-btn"/> 
            <?php if(isset($data['id'])&&$data['id']>0): ?><a href="<?php echo $data['url']; ?>" target="_blank" title="voir la page" class="pwe-ui-btn">voir</a><?php endif; ?>
        </div>
        <?php echo $this->getInputHtml('Titre', 'modele_title', (isset($data['title']))?$data['title']:'', 'page', (isset($data['id']))?$data['id']:0, array('class_input'=>'titreModele','actions'=>array('onblur'=>"javascript:generatePermalien($(this).val());"))); ?>
        <?php echo $this->getInputHtml('Permalien', 'modele_lien', (isset($data['permalien']))?$data['permalien']:'', 'page', (isset($data['id']))?$data['id']:0, array('prefix'=>PWE::getBaseURL(),'sufix'=>modele_permalien::getExtensionByModele('page'),'id_input'=>'permalien','actions'=>array('onblur'=>"javascript:validPermalien($(this).val());"))); ?>
        <?php echo $this->getTextareaHtml('Contenu', 'modele_content', (isset($data['content']))?$data['content']:'', 'page', (isset($data['id']))?$data['id']:0, array('class_input'=>'editor')); ?>
        <?php echo $this->getTextareaHtml('Résumé', 'modele_summary', (isset($data['summary']))?$data['summary']:'', 'page', (isset($data['id']))?$data['id']:0, array('class_input'=>'editor')); ?>
        
        <?php include('inc/seoManager.inc.php'); ?>
        <?php include('inc/redirectionManager.inc.php'); ?>
        <div align="right">
            <input type="submit" value="Enregistrer" class="pwe-ui-btn"/> 
            <?php if(isset($data['id'])&&$data['id']>0): ?><a href="<?php echo $data['url']; ?>" target="_blank" title="voir la page" class="pwe-ui-btn">voir</a><?php endif; ?>
        </div>
        
    </div>
    <div class="columns four">
        <?php include('inc/statutManager.inc.php'); ?>
        <br/><br/>
        <?php include('inc/templateManager.inc.php'); ?>
        <br/><br/>
        <h3>est une sous-page de</h3>
        <select name="modele_parent_id">
            <option value="0">racine du site</option>
            <?php foreach(modele_category::getPrimaryListe() as $category): ?>
            <?php
                if(!isset($data['category_id'])) $data['category_id'] = 0;
                if( isset($data['parent_id']) && $data['parent_id'] == $category->getId()){
                    $selected = ' selected="selected"';
                }else{
                    $selected = '';
                }
            ?>
            <option <?php echo ($category->getId()==$data['category_id'])?'class="desable"':'value="'.$category->getId().'"'; ?><?php echo $selected; ?>><?php echo $category->getTitle(); ?></option>
                <?php if($category->haveChild()): ?>
                <?php foreach($category->getChild() as $sCategory): ?>
                <?php
                    if(isset($data['parent_id']) && $data['parent_id'] == $sCategory->getId()){
                        $selected = ' selected="selected"';
                    }else{
                        $selected = '';
                    }
                ?>
                <option <?php echo ($sCategory->getId()==$data['category_id'])?'class="desable"':'value="'.$sCategory->getId().'"'; ?><?php echo $selected; ?>> - <?php echo $sCategory->getTitle(); ?></option>
                
                    <?php if($sCategory->haveChild()): ?>
                    <?php foreach($sCategory->getChild() as $ssCategory): ?>
                    <?php
                        if(isset($data['parent_id']) && $data['parent_id'] == $ssCategory->getId()){
                            $selected = ' selected="selected"';
                        }else{
                            $selected = '';
                        }
                    ?>
                    <option <?php echo ($ssCategory->getId()==$data['category_id'])?'class="desable"':'value="'.$ssCategory->getId().'"'; ?><?php echo $selected; ?>> - - <?php echo $ssCategory->getTitle(); ?></option>
                    <?php endforeach; ?>
                    <?php endif; ?>
                <?php endforeach; ?>
                <?php endif; ?>
            
            <?php endforeach;?>
        </select><br/><br/>
        <?php include('inc/mediaManager.inc.php'); ?>
        <br/><br/>
            <?php include('inc/galerieManager.inc.php'); ?>
        <br/><br/>
        <?php include('inc/tagManager.inc.php'); ?>
        <input type="hidden" name="modele_category_id" value="<?php echo (isset($data['category_id']))?$data['category_id']:0; ?>" />
        <input type="hidden" name="modele_id" value="<?php echo (isset($data['id']))?$data['id']:0; ?>" />
    </div>
</form>
<?php else: ?>

<div class="columns thirteen">
    <h1>Liste des pages</h1>
    <hr style="margin: 5px 0 25px 0;">
    <div align="right">
        <a class="pwe-ui-btn" href="?template=page&modele=page&action=create">Créer une page</a>
    </div>
    <br/>
    <table class="liste ui-widget table_sortable" width="100%">
        <thead class="ui-widget-header">
            <tr>
                <th></th>
                <th>Titre</th>
                <th>Modèle</th>
                <th>Parent</th>
                <th>Dernière modification</th>
                <th>Auteur</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody class="ui-widget-content">
            <?php foreach(modele_page::getListe() as $idx=>$page): ?>
            <tr>
                <td><?php echo $idx; ?></td>
                <td>
                    <span><?php echo $page->getTitle(); ?></span><br/>
                </td>
                <td><?php echo $page->getTemplate(); ?></td>
                <td>
                    <?php if($page->haveParent()){
                        echo $page->getParent()->getTitle().' ('.$page->getParent()->type.')';
                    }else{
                        echo '-';
                    }?>
                    
                </td>
                <td>
                    <?php echo $page->getDate(); ?><br/>
                    <b><?php echo $page->getStatut(); ?></b>
                </td>
                <td>
                    <b><?php echo $page->getAuteur(); ?></b><br/>
                    (<?php echo $page->getAuteurLevel(); ?>)
                </td>
                <td>
                    <a class="pwe-ui-btn" href="<?php echo $page->getUrl(); ?>" target="preview_site" title="Voir"><span class="ui-icon ui-icon-extlink"></span></a>
                    <a class="pwe-ui-btn"  href="?template=page&modele=page&action=update&modele_id=<?php echo $page->getId(); ?>" title="Modifier"><span class="ui-icon ui-icon-pencil"></span></a>
                    <a href="?template=page&modele=page&action=delete&do=1&modele_id=<?php echo $page->getId(); ?>" class="confirmAction pwe-ui-btn" title="Supprimer la page '<?php echo $page->getTitle(); ?>'"><span class="ui-icon ui-icon-trash"></span></a>
                </td>
            </tr>
            <?php if($page->haveChild()): ?>
                <?php foreach($page->getChild() as $scategory): ?>
                <?php if($scategory->isPage()): ?>
                <?php
                if($scategory->type==='category'){    
                    $pubs = $scategory->getPublications();
                    $pub = $pubs[0];
                    $spage = new modele_page($pub->getId());
                }else{
                    $spage = $scategory;
                }
                ?>
                <tr class="sous-rubrique">
                    <td><?php echo $idx; ?> - </td>
                    <td>
                        <span><?php echo $spage->getTitle(); ?></span><br/>
                    </td>
                    <td><?php echo $spage->getTemplate(); ?></td>
                    <td>
                        <?php echo $spage->getDate(); ?><br/>
                        <b><?php echo $spage->getStatut(); ?></b>
                    </td>
                    <td>
                        <b><?php echo $spage->getAuteur(); ?></b><br/>
                        (<?php echo $spage->getAuteurLevel(); ?>)
                    </td>
                    <td>
                        <a class="pwe-ui-btn" href="<?php echo $spage->getUrl(); ?>" target="preview_site" title="Voir"><span class="ui-icon ui-icon-extlink"></span></a>
                        <a class="pwe-ui-btn"  href="?template=page&modele=page&action=update&modele_id=<?php echo $spage->getId(); ?>" title="Modifier"><span class="ui-icon ui-icon-pencil"></span></a>
                        <a href="?template=page&modele=page&action=delete&do=1&modele_id=<?php echo $spage->getId(); ?>" class="confirmAction pwe-ui-btn" title="Supprimer la page '<?php echo $spage->getTitle(); ?>'"><span class="ui-icon ui-icon-trash"></span></a>
                    </td>
                </tr>
                <?php endif; ?>
                <?php if(isset($spage)&&$spage->haveChild()): ?>
                    <?php foreach($spage->getChild() as $sscategory): ?>
                    <?php if($sscategory->isPage()): ?>
                    <?php
                    if($sscategory->type==='category'){
                        $spubs = $sscategory->getPublications();
                        $spub = $spubs[0];
                        $sspage = new modele_page($spub->getId());
                    }else{
                        $sspage = $sscategory;
                    }
                    ?>
                    <tr class="sous-sous-rubrique">
                        <td><?php echo $idx; ?> --</td>
                        <td>
                            <span><?php echo $sspage->getTitle(); ?></span>
                        </td>
                        <td><?php echo $sspage->getTemplate(); ?></td>
                        <td>
                            <?php echo $sspage->getDate(); ?><br/>
                            <b><?php echo $sspage->getStatut(); ?></b>
                        </td>
                        <td>
                            <b><?php echo $sspage->getAuteur(); ?></b><br/>
                            (<?php echo $sspage->getAuteurLevel(); ?>)
                        </td>
                        <td>
                            <a class="pwe-ui-btn" href="<?php echo $sspage->getUrl(); ?>" target="preview_site" title="Voir"><span class="ui-icon ui-icon-extlink"></span></a>
                            <a class="pwe-ui-btn"  href="?template=page&modele=page&action=update&modele_id=<?php echo $sspage->getId(); ?>" title="Modifier"><span class="ui-icon ui-icon-pencil"></span></a>
                            <a href="?template=page&modele=page&action=delete&do=1&modele_id=<?php echo $sspage->getId(); ?>" class="confirmAction pwe-ui-btn" title="Supprimer la page '<?php echo $sspage->getTitle(); ?>'"><span class="ui-icon ui-icon-trash"></span></a>
                        </td>
                    </tr>
                    <?php endif; ?>
                    <?php endforeach; ?>
                <?php endif; ?>
            <?php endforeach; ?>
            <?php endif; ?>
            <?php endforeach; ?>
        </tbody>
        <tfoot></tfoot>
    </table>
</div>
<?php endif; ?>